# Date- 5/3/23
# Script No. 2
# Name: MODELS
# File target: 1) Regressing activists on representation gap 
#              2) Regressing activists on party extremity
#              3) Regressing activists on party polarization
#              4) Robustness checks 

library(foreign)
library(stargazer)
library(sandwich)
library(broom)
library(lmtest)
library(Ecdat)
library(estimatr)
library(jtools)
library(huxtable)
library(sjPlot)
library(sjmisc)
library(tidyr)
library(pequod)
library(fastDummies)
library(lme4)
library(lmerTest)
library(psycho)
library(plm)
library(lmtest)
library(multiwayvcov)

# setwd("C:/Users/yaira/Desktop/ideological polarization/FINAL/Do and data")
# ppdb.fin <- read_csv("ppdb.fin.csv")

###################################################################################################################
######################################## (1) Representation #######################################################
###################################################################################################################
setwd("C:/Users/yaira/Desktop/ideological polarization/FINAL/Tables and Graphs")

######################### mdoel ########################
## 1) the effet of activists on representation: act with wights, party position by voters

mod.rep1 <- lm(party.pol ~ activists * mean.P,
               data = ppdb.fin)

summary(mod.rep1)

mod.rep2 <- lm(party.pol ~ activists * mean.P+
                 GALLSQ + ENP + medleg + elffrn,
               data = ppdb.fin)
summary(mod.rep2)


# TABLE 2 in the paper
stargazer(mod.rep1, mod.rep2,
          title="TABLE 2 - The Effect of Party Activists on ideological incongruence", align=TRUE,
          dep.var.labels=c(""), 
          omit.stat=c("LL","ser","f"), no.space=TRUE, type = 'html', out = "table 2- representation.htm")



###################################################################################################################
################################################# (2) Party extremity  ############################################ 
###################################################################################################################

# activists on party extremity (table 1)

ppdb.fin$extrem.P <- abs(ppdb.fin$mean.P-5)
summary(ppdb.fin$extrem.P)

ppdb.fin$extrem.V <- abs(ppdb.fin$mean.V-5)
summary(ppdb.fin$extrem.V)

mod.ex1 <- lm(party.extremity ~ activists,
              data = ppdb.fin)
summary(mod.ex1)

mod.ex2 <- lm(party.extremity ~ activists +
                GALLSQ + ENP + medleg + elffrn,
              data = ppdb.fin)
summary(mod.ex2)


mod.ex3 <- lm(party.extremity ~ activists + 
                extrem.V +
                GALLSQ + ENP + medleg + elffrn,
              data = ppdb.fin)
summary(mod.ex3)

mod.ex4 <- lm(party.extremity ~ activists + 
                extrem.P +
                GALLSQ + ENP + medleg + elffrn,
              data = ppdb.fin)
summary(mod.ex4)

mod.ex5 <- lm(party.extremity ~ activists * extrem.P +
                GALLSQ + ENP + medleg + elffrn,
              data = ppdb.fin)
summary(mod.ex5)


stargazer(mod.ex1, mod.ex2, mod.ex3, mod.ex4, mod.ex5,  
          title= "Table 1 - The Effect of Party Activists on Party Extremity", align=TRUE,
          dep.var.labels=c("","Cluster"), 
          omit.stat=c("LL","ser","f"), no.space=TRUE, type = 'html', out = "table 1- party extremity.htm")

#
library(interactions)
johnson_neyman(mod.ex5, pred = activists, modx = extrem.P, alpha = .05)
#


###################################################################################################################
################################################# (3) Party polarization  ######################################### 
###################################################################################################################

# activists on party polarization (table 3)

mod.pol.1 <- lm(party.polarization ~ coun.act,
                data = ppdb.fin.coun)
summary(mod.pol.1)

mod.pol.2 <- lm(party.polarization ~ coun.act +
                  GALLSQ + ENP + medleg + elffrn,
                data = ppdb.fin.coun)
summary(mod.pol.2)


stargazer(mod.pol.1, mod.pol.2, 
          title="Table 3 -The Effect of Party Activists on Party Polarization", align=TRUE,
          dep.var.labels=c(""), covariate.labels=c("Activists' Relative Power", "Propostionality", "ENP", "MedD(lagged)", "Elffrn"),
          omit.stat=c("LL","ser","f"), no.space=TRUE, type = 'html', out = "table 3- party polarization.htm")

# activists on party polarization without weights (table 3)

mod.pol.1 <- lm(party.polarization ~ coun.act2,
                data = ppdb.fin.coun)
summary(mod.pol.1)

mod.pol.2 <- lm(party.polarization ~ coun.act2 +
                  GALLSQ + ENP + medleg + elffrn,
                data = ppdb.fin.coun)
summary(mod.pol.2)


stargazer(mod.pol.1, mod.pol.2, 
          title="Table A7 -The Effect of Party Activists on Party Polarization", align=TRUE,
          dep.var.labels=c(""), covariate.labels=c("Activists' Relative Power", "Propostionality", "ENP", "MedD(lagged)", "Elffrn"),
          omit.stat=c("LL","ser","f"), no.space=TRUE, type = 'html', out = "table A7- party polarization.htm")
